package banwood.com.xiongfasy.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import banwood.com.xiongfasy.BASE.BaseActivity;
import banwood.com.xiongfasy.R;
import banwood.com.xiongfasy.bean.BillDetail;
import banwood.com.xiongfasy.dialog.LoadingDialog;
import banwood.com.xiongfasy.http.GetAsynctask;
import banwood.com.xiongfasy.listener.ResultListener;
import banwood.com.xiongfasy.utils.LogUtils;
import butterknife.BindView;
import butterknife.OnClick;
import cn.shellinfo.serv.comm.ParamMap;



/*
 * 退款页面
 * */
public class TuiKuaiActivity extends BaseActivity {
    private static String TAG="TuiKuaiActivity";
    @BindView(R.id.tuikuai_hao)
    EditText tuikuaiHao;
    @BindView(R.id.tuikuai_start)
    Button tuikuaiStart;
    private LoadingDialog dialog;

    @Override
    protected int getLayoutId() {
        return R.layout.tuikuai_xml;
    }

    @Override
    protected void initView() {
        settitle("退款");

    }


    @Override
    protected void initData() {


    }

    @OnClick(R.id.tuikuai_start)
    public void tuikuaiOnclick(View v){
        if(!TextUtils.isEmpty(tuikuaiHao.getText().toString().trim())){
            startQuery(tuikuaiHao.getText().toString());
        }else{
            Toast.makeText(TuiKuaiActivity.this,"请输入流水号",Toast.LENGTH_SHORT).show();
        }
    }


    private void startQuery(String msg) {
        dialog=new LoadingDialog(TuiKuaiActivity.this,1);
        dialog.setMessage("正在退款,请稍后....");
        dialog.show();

        ParamMap paramMap=new ParamMap();
        paramMap.put("PayTypeID","ImmediatePay");
//        paramMap.put("OrderNo","ON1495699207766");
        paramMap.put("OrderNo",msg);
        paramMap.put("QueryDetail","1");

        new GetAsynctask(new ResultListener() {
            @Override
            public void getResult(ParamMap result) {

                if(result!=null && result.getString("ReturnCode").equals("0000")){
                    LogUtils.d(TAG,result.toString());
                    String odrenInfo=result.getString("orderDetail");
                    Gson gson=new Gson();
                    BillDetail info=new BillDetail();
                    info=gson.fromJson(odrenInfo,BillDetail.class);
                   //查到了有这个流水号的存在，继续退款
                    getRefunde(info);

                }else{
                    dialog.dismiss();
//                    Toast.makeText(TuiKuaiActivity.this,"查询失败",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(TuiKuaiActivity.this,QueryFailActivity.class);
                    intent.putExtra("title","退款失败");
                    startActivity(intent);
                }


            }
        },TuiKuaiActivity.this,paramMap,"getQueryOrder").execute(paramMap);


    }

    private void getRefunde(BillDetail info){

        ParamMap paramMap=new ParamMap();
        paramMap.put("txtOrderDate",info.getOrderDate());
        paramMap.put("txtOrderTime",info.getOrderTime());
        paramMap.put("txtOrderNo",info.getOrderNo());
        paramMap.put("txtNewOrderNo","newON"+System.currentTimeMillis());
        paramMap.put("txtCurrencyCode","156");
        paramMap.put("txtTrxAmount",info.getOrderAmount());


        new GetAsynctask(new ResultListener() {
            @Override
            public void getResult(ParamMap result) {
                dialog.dismiss();
                if(result!=null){
                    Intent intent=new Intent(TuiKuaiActivity.this,RefundSucActivity.class);
                    intent.putExtra("param",result);
                    startActivity(intent);
                    tuikuaiHao.setText("");
                }else{
                    Intent intent=new Intent(TuiKuaiActivity.this,QueryFailActivity.class);
                    intent.putExtra("title","退款失败");
                    startActivity(intent);
                }
            }
        },TuiKuaiActivity.this,paramMap,"getRefunds").execute(paramMap);


    }


}
